/**
 * 
 */
package db;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * @author Andrew
 * 
 */
public class FormatAndValidation {

	
	
	/**
	 * Error message
	 */
	private static String errorMessage = "";
	
	
	/**
	 * 
	 * @param dirtyPhoneNumber
	 * @return String with only integers
	 */
	public static String cleanPhoneNumber(String dirtyPhoneNumber) {

		String cleanPhoneNumber = "";

		cleanPhoneNumber = dirtyPhoneNumber.replaceAll("[^0-9]", "");

		return cleanPhoneNumber;

	}
	
	public static String formatPhoneNumber(String pNumber){
		String fNumber = "";
		
		fNumber = "(" + pNumber.substring(0,3) + ") " + pNumber.substring(3,6) + "-" + pNumber.substring(6,10);

		return fNumber;
	}

	/**
	 * 
	 * @param unknownPhoneNumber
	 * @return boolean true if phone number is valid, otherwise return false
	 */
	public static boolean validPhoneNumber(String unknownPhoneNumber) {

		boolean isValid = false;

		if (cleanPhoneNumber(unknownPhoneNumber).length() == 10) {
			isValid = true;
		}

		return isValid;
	}

	/**
	 * format date to string
	 */
	public static String convertDate(String d) {
		if (d == null || d == "") {
			return "";
		}
		String newStrDate = "";
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

		Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		java.util.Date newDate = null;

		try {
			newDate = df.parse(d);

		} catch (ParseException e) {
			System.out.println(e.getMessage());
			return newStrDate;
		}
		System.out.println(formatter.format(newDate));

		return formatter.format(newDate);

	}

	/**
	 * format date for HTML string
	 */
	public String convertStrDateForHtml(String d) {
		if (d == null || d == "") {
			return "";
		}
		String newStrDate = "";
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

		Format formatter = new SimpleDateFormat("MM/dd/yyyy");

		java.util.Date newDate = null;

		try {
			newDate = df.parse(d);

		} catch (ParseException e) {
			System.out.println(e.getMessage());
			return newStrDate;
		}
		System.out.println(formatter.format(newDate));

		return formatter.format(newDate);

	}

	/**
	 * format date for HTML string
	 */
	public String convertDateForHtml(Date d) {
		if (d == null) {
			return "";
		}

		Format formatter = new SimpleDateFormat("MM/dd/yyyy");

		System.out.println(formatter.format(d));

		return formatter.format(d);

	}

	/**
	 * convert password
	 * 
	 * @param String
	 *            input
	 * 
	 */
	public static String md5(String input) {

		String md5 = null;

		if (null == input)
			return null;

		try {

			// Create MessageDigest object for MD5
			MessageDigest digest = MessageDigest.getInstance("MD5");

			// Update input string in message digest
			digest.update(input.getBytes(), 0, input.length());

			// Converts message digest value in base 16 (hex)
			md5 = new BigInteger(1, digest.digest()).toString(16);

		} catch (NoSuchAlgorithmException e) {

			e.printStackTrace();
		}
		return md5;
	}

	public static String md5Convert(byte[] input) {

		String md5Reversed = null;

		if (null == input)
			return null;

		// Converts bites to string
		md5Reversed = UUID.nameUUIDFromBytes(input).toString();
		return md5Reversed;
	}
	
	/** 
	 * validate format
	 */	
	public static Boolean validateUserDate(String d){
		try {
			System.out.println(d);
			String t = "tst";
			System.out.println(t);
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
			java.util.Date dateGMT = null;
			//formatter = new SimpleDateFormat("MM/dd/yyyy");
			dateGMT = sdf.parse(d);
			System.out.println(dateGMT);
			errorMessage = "";
			return true;
			
		}catch (Exception e)
		{
			errorMessage = " should be in this format mm/dd/yyyy";
			System.out.println("Error in validatedate: " +
					e.getClass().getName()+": "+ e.getMessage());
			return false;
			
		}
		
	}
	
	
	
	
	public static String getErrorMessage() {
		return errorMessage;
	}
	public static void setErrorMessage(String InErrorMessage) {
		errorMessage = InErrorMessage;
	}
	
}
